package com.jeeplus.test.jlnu.deanOffice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jeeplus.test.jlnu.admissionInfo.domain.entity.Supplement;
import com.jeeplus.test.jlnu.deanOffice.domain.vo.JlnuDeansOfficeDto;
import com.jeeplus.test.jlnu.deanOffice.service.DeansOfficeService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "教务处信息")
@RestController
@Slf4j
@RequestMapping("/deansOfficeInfo")
public class DeanOfficeController {

    @Autowired
    private DeansOfficeService deansOfficeService;

    @ApiOperation("教务处分页查询")
    @GetMapping("/page")
    public ResponseEntity<Page<Map<String, Object>>> getAdmissionInfoPage(JlnuDeansOfficeDto jlnuDeansOfficeDto,
                                                                          @RequestParam(required = true) String officeId,
                                                                          @RequestParam(defaultValue = "1") int pageNum,
                                                                          @RequestParam(defaultValue = "10") int pageSize) {
        Page<JlnuDeansOfficeDto> page = new Page<>(pageNum, pageSize);
        Page<Map<String, Object>> pageResult = deansOfficeService.getAdmissionInfoPage(jlnuDeansOfficeDto, page,officeId);

        // 确保 null 值字段被填充
        List<Map<String, Object>> modifiedRecords = pageResult.getRecords().stream()
                .map(record -> {
                    Map<String, Object> modifiedRecord = new HashMap<>();
                    record.forEach((key, value) -> modifiedRecord.put(key, value != null ? value : ""));
                    return modifiedRecord;
                })
                .collect(Collectors.toList());

        Page<Map<String, Object>> modifiedPage = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        modifiedPage.setRecords(modifiedRecords);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(modifiedPage);
    }

    @ApiOperation("教务处修改信息")
    @PostMapping("/update")
    public R update(@RequestBody JlnuDeansOfficeDto jlnuDeansOfficeDto){

        deansOfficeService.update(jlnuDeansOfficeDto);
        return R.suc();
    }

    @ApiOperation("根据学号查询")
    @GetMapping("/selectById")
    public R selectNullByStuId(@RequestParam String stuId){
        if(StringUtils.isEmpty(stuId)){
            return R.error("9999","参数为空");
        }else {
            JlnuDeansOfficeDto supplement = deansOfficeService.selectDeanOfficeById(stuId);
            return R.suc(supplement);
        }
    }

}
